from django.shortcuts import render
from  django.http import HttpResponse
from hn import get_k3_data
from hn import models
from django.core import serializers
from itertools import chain
from django.db.models import Q
# Create your views here.

def index(request):

    return render(request,'index.html')
def find(request):
    if request.method == "POST":
        # print(request.get_port())
        code0 = request.POST.get('code0')
        code1 = request.POST.get('code1')
        code2 = request.POST.get('code2')
        # print(code0, code1, code2)
        # search_data = models.k3_hn.objects.all(code_0=code0,code_1=code1,code_2=code2)
        search_data = models.k3_hn.objects.filter(code_0=code0,code_1=code1,code_2=code2).values()
        search_list = list(search_data) #将查询结果的QuerySet 转换为list，list可以转为json
        # print(search_list)
        # return HttpResponse(json.dumps(search_list),content_type='application/json')
        return render(request,'find.html')

def update_data(request):
    max_sql = models.k3_hn.objects.latest('id')  #数据库ID最大列
    max_sql_lusse = max_sql.lusse    #最大列的期号
    print('数据库最大列期号为',max_sql_lusse)

    update_date = get_k3_data.k3_hn_today() #爬取今天k3网站的数据，update_dete是数据列表
    # update_date = get_k3_data.k3_hn_yesterday()
    update_last_lusse = update_date[-1][0]   #列表里最后一列的第一个值为k3网站最新的开奖期数
    print(update_last_lusse)
    # if int(max_sql_lusse) >= int(update_last_lusse) :  #
        # models.k3_hn.objects.get(lusse=yesterday_last_lusse) #查询到有昨天数据结束，查询不到程序报错
        # return HttpResponse('开奖数据已经是最新，不需要再次获取')
    for data_list in update_date:
        print(data_list)
        new_lusse = data_list[0]
        print(new_lusse,data_list[1])
        if int(new_lusse) > int(max_sql_lusse):  #取到的期号大于数据库中最大期号的值
            repeat = set()
            # code_1_repeat = models.k3_hn.objects.filter(code_0=data_list[1]).filter(code_1=data_list[1]).filter(code_2=data_list[1])
            #查询3个开奖号码是否在数据库中存在
            code_repeat = models.k3_hn.objects.filter(Q(code_0=data_list[1])|Q(code_1=data_list[1])|Q(code_2=data_list[1])|Q(code_0=data_list[2])|Q(code_1=data_list[2])|Q(code_2=data_list[2])|Q(code_0=data_list[3])|Q(code_1=data_list[3])|Q(code_2=data_list[3]))
            # three_req = chain(code_1_repeat,code_2_repeat,code_3_repeat)  #将三条查询结果组合一起
            # print(code_repeat.values())
            for sql_lusse_dict in code_repeat.values():
                sql_lusse = sql_lusse_dict.get('lusse')
                num = int(new_lusse)-int(sql_lusse)
                if num < 8:
                    repeat.add(num)
                elif num > 7:
                    repeat.add(10)
            print(repeat)
            double_num_lusse = one_num_lusse = two_num_lusse = three_num_lusse = four_num_lusse =five_num_lusse = six_num_lusse = super_six_num_lusse =' '
            if 1 in repeat:
                double_num_lusse = 1  #重号
            if 2 in repeat:
                one_num_lusse = 2  #一期号
            if 3 in repeat:
                two_num_lusse = 3 #二期号
            if 4 in repeat:
                three_num_lusse = 4  #三期号
            if 5 in repeat:
                four_num_lusse = 5   #四期号
            if 6 in repeat:
                five_num_lusse = 6  #五期号
            if 7 in repeat:
                six_num_lusse = 7  #六期号
            if 10 in repeat:
                super_six_num_lusse = 10  #超六期号
            print('------------')
            obj = models.k3_hn(lusse=data_list[0],code_0=data_list[1],code_1=data_list[2],code_2=data_list[3],double_num=double_num_lusse,one_num=one_num_lusse,two_num=two_num_lusse,three_num=three_num_lusse,four_num=four_num_lusse,five_num=five_num_lusse,six_num=six_num_lusse,super_six_num=super_six_num_lusse)
            obj.save()
    return HttpResponse('获取最新开奖数据成功')

def search_all(request):
    if request.method == "POST":
        data_all = models.k3_hn.objects.order_by('-id')[:100] #取查询结果的最后100条

        data_all_json = serializers.serialize('json',data_all)
        print(data_all_json)
        return  HttpResponse(data_all_json,content_type='application/json')
    if request.method == "GET":
        data_all = models.k3_hn.objects.order_by('-id')[:100]  # 取查询结果的最后100条
        data_all_json = serializers.serialize('json', data_all)
        print(data_all_json)
        return HttpResponse(data_all_json, content_type='application/json')